//
//  LiverHeadView.m
//  Bread-Live
//
//  Created by ios on 2017/3/13.
//  Copyright © 2017年 Chris. All rights reserved.
//

#import "LiverHeadView.h"

@interface LiverHeadView ()

@property(nonatomic,strong) UIImageView *bgImageView;
@property(nonatomic,strong) UIImageView *headImageView;
@property(nonatomic,strong) UILabel     *numberLabel;
@property(nonatomic,strong) UILabel     *titleLabel;
@property(nonatomic,strong) UILabel     *descritionLabel;
@property(nonatomic,strong) UIButton    *followBtn;

@property(nonatomic,strong) UIView      *blackView;

@property(nonatomic,assign) CGFloat      height;

@end

@implementation LiverHeadView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _height = 0;
        
        [self configView];
    }
    return self;
}


-(void)configView{

    CGFloat viewW = self.frame.size.width ;
    CGFloat viewH = self.frame.size.height;
    
    _bgImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewW, viewH)];
    [self addSubview:_bgImageView];
    
    _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewW, viewH)];
    _blackView.backgroundColor = [UIColor blackColor];
    _blackView.alpha = 0.3;
    
    [self insertSubview:_blackView aboveSubview:_bgImageView];
    
    _headImageView    = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/2-30, 30, 60, 60)];
    _headImageView.layer.cornerRadius = 6;
    _headImageView.clipsToBounds = YES;
    
    [self addSubview:_headImageView];
    
    _numberLabel    = [[UILabel alloc] initWithFrame:CGRectMake(kWidth-150, 10, 145, 21)];
    _numberLabel.textAlignment   = NSTextAlignmentRight;
    _numberLabel.backgroundColor = [UIColor clearColor];
    _numberLabel.font            = [UIFont wyNormalFot:14];
    _numberLabel.textColor       = WYCOLOR(170, 170, 170, 1.0);
    
    [self addSubview:_numberLabel];
    
    _titleLabel       = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, viewW - 40,21)];
    _titleLabel.font  = [UIFont wyBlodFont:15];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor       = [UIColor whiteColor];
    _titleLabel.textAlignment   = NSTextAlignmentCenter;
    
    [self addSubview:_titleLabel];
    
    _followBtn = [UIButton createCustomButtonWithFrame:CGRectMake(kWidth/2-50, 140, 100, 32) title:@"关注主讲人" backGroungColor:[UIColor clearColor] titleColor:[UIColor whiteColor] font:[UIFont wyNormalFot:14]];
    _followBtn.layer.borderWidth = 1.0;
    _followBtn.layer.cornerRadius = 6;
    _followBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    _followBtn.layer.masksToBounds = YES;
    
    [_followBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [_followBtn setTitle:@"已关注" forState:UIControlStateSelected];
    
    [_followBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_followBtn];
    
}

-(void)setDescritionText:(NSString *)string{
    
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;
    
    CGFloat height1 = [ToolsCustom getSpaceLabelHeight:string withFont:[UIFont wyNormalFot:14] withWidth:kWidth-40 lineSpacing:5];
    
    _height = height1;
    
    _bgImageView.frame = CGRectMake(0, 0, viewW, viewH);
    _blackView.frame   = CGRectMake(0, 0, viewW, viewH);
    
    _followBtn.frame = CGRectMake(kWidth/2-50, 130+height1+10, 100, 32);
    
    _descritionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, viewW-40, height1)];
    
    _descritionLabel.textColor = [UIColor whiteColor];
    _descritionLabel.numberOfLines = 0;
    _descritionLabel.backgroundColor = [UIColor clearColor];
    
    [ToolsCustom setLabelSpace:_descritionLabel withValue:string withFont:[UIFont wyNormalFot:14] lineSpacing:5];
    
    [self addSubview:_descritionLabel];
    
}


-(void)btnClick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        sender.frame = CGRectMake(kWidth/2-30, 140+ _height, 60, 32);
        sender.backgroundColor = WYCOLOR(140, 140, 139, 1.0);
        sender.layer.borderWidth = 0.5;
        
    }else{
        sender.frame = CGRectMake(kWidth/2-50, 140+ _height, 100, 32);
        sender.backgroundColor = [UIColor clearColor];
        sender.layer.borderWidth = 1.0;
    }
    
}

-(void)layoutSubviews{
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;
    
    
    
}


-(void)setModel:(HotLiverModel *)model{
    
    [_bgImageView sd_setImageWithURL:[NSURL URLWithString:model.bgUrl]];
   
 //   [_headImageView sd_setImageWithURL:[NSURL URLWithString:model.headImgUrl]];
    
    _headImageView.image = [UIImage imageNamed:@"user.jpg"];
    
    if (model.descritionText.length > 0) {
        _titleLabel.text = model.title;
    }else{
        _titleLabel.text = model.name;
    }
    
    _numberLabel.text = [NSString stringWithFormat:@"· %@粉丝",model.fansNumber];
    
    _followBtn.selected = model.isfollow;
    if (_followBtn.selected) {
        _followBtn.frame = CGRectMake(kWidth/2-30, 140+ _height, 60, 32);
        _followBtn.backgroundColor   = WYCOLOR(140, 140, 139, 1.0);
        _followBtn.layer.borderWidth = 0.5;
        
    }else{
        _followBtn.frame = CGRectMake(kWidth/2-50, 140+ _height, 100, 32);
        _followBtn.backgroundColor   = [UIColor clearColor];
        _followBtn.layer.borderWidth = 1.0;
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
